package com.wdb007.venus.web.api.controller;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.facade.CardService;
import com.wdb007.venus.web.api.card.model.ChargeRequest;
import com.wdb007.venus.web.api.model.BaseResponse;

@Controller
@RequestMapping("/card")
public class CardController extends BaseController {

	private final static Logger logger = LoggerFactory.getLogger(CardController.class);

	@Autowired
	private CardService cardService;
	
	@RequestMapping(value="/charge", method=RequestMethod.POST)
	@ResponseBody
	public BaseResponse charge(ChargeRequest request) {
		
		logger.info("充值码充值接口charge request: {}", JSON.toJSONString(request));
		BaseResponse baseResponse = success();
		try {
			checkIsNull(request.getCode(), "code");
			BigDecimal bd = cardService.charge(request.getUserid(), request.getCode());
			int amount = (int)bd.doubleValue();
			baseResponse.setMessage("恭喜你" + amount + "元已经充值到您的账户中,请在消费记录中查看");
		}catch(Exception e) {
			logger.error(e.getMessage(), e);
			baseResponse = failed(e);
		}

		logger.info("充值码充值接口charge response: {}", JSON.toJSONString(baseResponse));
		return baseResponse;
		
	}
}
